package com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationreside;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationreside.vo.AccommodationResideCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationreside.vo.AccommodationResideExcelVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationreside.vo.AccommodationResideExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationreside.vo.AccommodationResidePageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationreside.vo.AccommodationResideRespVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationreside.vo.AccommodationResideUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.accommodationreside.AccommodationResideConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.accommodationreside.AccommodationResideDO;
import com.nanhai.buddhist.college.module.collegebase.service.accommodationreside.AccommodationResideService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 入住记录")
@RestController
@RequestMapping("/collegebase/accommodation-reside")
@Validated
public class AccommodationResideController {

    @Resource
    private AccommodationResideService accommodationResideService;

    @PostMapping("/create")
    @ApiOperation("创建入住记录")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-reside:create')")
    public CommonResult<String> createAccommodationReside(@Valid @RequestBody AccommodationResideCreateReqVO createReqVO) {
        return success(accommodationResideService.createAccommodationReside(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新入住记录")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-reside:update')")
    public CommonResult<Boolean> updateAccommodationReside(@Valid @RequestBody AccommodationResideUpdateReqVO updateReqVO) {
        accommodationResideService.updateAccommodationReside(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除入住记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-reside:delete')")
    public CommonResult<Boolean> deleteAccommodationReside(@RequestParam("id") String id) {
        accommodationResideService.deleteAccommodationReside(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得入住记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-reside:query')")
    public CommonResult<AccommodationResideRespVO> getAccommodationReside(@RequestParam("id") String id) {
        AccommodationResideDO accommodationReside = accommodationResideService.getAccommodationReside(id);
        return success(AccommodationResideConvert.INSTANCE.convert(accommodationReside));
    }

    @GetMapping("/list")
    @ApiOperation("获得入住记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-reside:query')")
    public CommonResult<List<AccommodationResideRespVO>> getAccommodationResideList(@RequestParam("ids") Collection<String> ids) {
        List<AccommodationResideDO> list = accommodationResideService.getAccommodationResideList(ids);
        return success(AccommodationResideConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得入住记录分页")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-reside:query')")
    public CommonResult<PageResult<AccommodationResideRespVO>> getAccommodationResidePage(@Valid AccommodationResidePageReqVO pageVO) {
        PageResult<AccommodationResideDO> pageResult = accommodationResideService.getAccommodationResidePage(pageVO);
        return success(AccommodationResideConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出入住记录 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-reside:export')")
    @OperateLog(type = EXPORT)
    public void exportAccommodationResideExcel(@Valid AccommodationResideExportReqVO exportReqVO,
                                               HttpServletResponse response) throws IOException {
        List<AccommodationResideDO> list = accommodationResideService.getAccommodationResideList(exportReqVO);
        // 导出 Excel
        List<AccommodationResideExcelVO> datas = AccommodationResideConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "入住记录.xls", "数据", AccommodationResideExcelVO.class, datas);
    }


    @PutMapping("/update/status")
    @ApiOperation("迁出")
    @ApiImplicitParam(name = "id", required = true, example = "1024,2048", dataTypeClass = String.class)
    public CommonResult<Boolean> updateAccommodationStatusReside(@RequestParam("id") String id) {
        accommodationResideService.updateAccommodationStatusReside(id);
        return success(true);
    }


    @PostMapping("/add")
    @ApiOperation("迁入")
    public CommonResult<String> addAccommodationReside(@Valid @RequestBody AccommodationResideCreateReqVO createReqVO) {
        return success(accommodationResideService.addAccommodationReside(createReqVO));
    }


}
